package com.ruoyi.fjtseac.service.impl;

import java.sql.Date;
import java.time.LocalDate;
import java.util.List;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.xss.XssValidator;
import com.ruoyi.fjtseac.config.OpenIdConfig;
import com.ruoyi.fjtseac.domain.dto.ApplicationFormDto;
import com.ruoyi.fjtseac.domain.po.ApplicationForm;
import com.ruoyi.fjtseac.service.UserService;
import com.ruoyi.fjtseac.util.DtoToPoMapper;
import com.ruoyi.fjtseac.util.GetPostUntil;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.fjtseac.mapper.ApplicationFormMapper;
import com.ruoyi.fjtseac.service.ApplicationFormService;

/**
 * 志愿者申请Service业务层处理
 *
 * @author ruoyi
 * @date 2022-07-16
 */
@Service
public class ApplicationFormServiceImpl implements ApplicationFormService {

    static java.util.logging.Logger log = java.util.logging.Logger.getLogger(String.valueOf(UserServiceImpl.class));

    @Autowired
    private ApplicationFormMapper applicationFormMapper;


    /**
     * 查询志愿者申请
     *
     * @param id 志愿者申请主键
     * @return 志愿者申请
     */
    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public ApplicationForm selectApplicationFormById(Long id) {
        return applicationFormMapper.selectApplicationFormById(id);
    }

    /**
     * 查询志愿者申请列表
     *
     * @param applicationFormDto 志愿者申请
     * @return 志愿者申请
     */
    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<ApplicationForm> selectApplicationFormList(ApplicationFormDto applicationFormDto) {
        ApplicationForm applicationForm = DtoToPoMapper.INSTANCE.toApplicationForm(applicationFormDto);
        return applicationFormMapper.selectApplicationFormList(applicationForm);
    }

    /**
     * 新增志愿者申请
     *
     * @param applicationFormDto 志愿者申请
     * @return 结果
     */
    @Override
    public AjaxResult insertApplicationForm(ApplicationFormDto applicationFormDto) {

        QueryWrapper<ApplicationForm> wrapper = new QueryWrapper<>();
        wrapper.eq("open_id", applicationFormDto.getOpenId());

        ApplicationForm applicationForm = applicationFormMapper.selectOne(wrapper);

        if (applicationForm == null) {
            applicationForm = DtoToPoMapper.INSTANCE.toApplicationForm(applicationFormDto);
            applicationForm.setReason("无");
            applicationFormMapper.insertApplicationForm(applicationForm);
            return AjaxResult.success("申请成功");
        } else if (applicationForm.getFlag() == 0) {//如果是第二次申请的话，删除旧记录，重新插入
            applicationFormMapper.deleteApplicationFormById(applicationForm.getId());
            applicationForm = DtoToPoMapper.INSTANCE.toApplicationForm(applicationFormDto);
            applicationForm.setReason("无");
            applicationFormMapper.insertApplicationForm(applicationForm);
            return AjaxResult.success("申请成功");
        } else if (applicationForm.getFlag() == 2) {
            return AjaxResult.success("您已经成为志愿者了");
        } else {
            return AjaxResult.error(1, "你已提交过申请");
        }

    }

    /**
     * 修改志愿者申请
     *
     * @param applicationFormDto 志愿者申请
     * @return 结果
     */
    @Override
    public int updateApplicationForm(ApplicationFormDto applicationFormDto) {
        ApplicationForm applicationForm = DtoToPoMapper.INSTANCE.toApplicationForm(applicationFormDto);
        return applicationFormMapper.updateApplicationForm(applicationForm);
    }

    /**
     * 批量删除志愿者申请
     *
     * @param ids 需要删除的志愿者申请主键
     * @return 结果
     */
    @Override
    public int deleteApplicationFormByIds(Long[] ids) {
        return applicationFormMapper.deleteApplicationFormByIds(ids);
    }

    /**
     * 删除志愿者申请信息
     *
     * @param id 志愿者申请主键
     * @return 结果
     */
    @Override
    public int deleteApplicationFormById(Long id) {
        return applicationFormMapper.deleteApplicationFormById(id);
    }

    @Override
    public AjaxResult refuse(ApplicationFormDto applicationFormDto) {

        if (XssValidator.containsHtml(applicationFormDto.getReason())) {//效验是否为Xss攻击（是否含脚本字符）
            return AjaxResult.error("拒绝理由不应该含有脚本语言");
        }

        ApplicationForm applicationForm = DtoToPoMapper.INSTANCE.toApplicationForm(applicationFormDto);
        applicationForm.setFlag(0);
        applicationForm.setOperationTime(Date.valueOf(LocalDate.now()));

        int flag = applicationFormMapper.updateById(applicationForm);

        return flag > 0 ? AjaxResult.success() : AjaxResult.error();
    }


    /**
     * 返回拒绝理由
     *
     * @param phone
     * @return
     */
    @Override
    public AjaxResult returnReason(String phone) {
        ApplicationForm applicationForm;

        if (phone != null) {
            applicationForm = applicationFormMapper.selectOne(
                    new QueryWrapper<ApplicationForm>()
                            .eq("phone", phone)
            );

            if (applicationForm != null) {
                if (applicationForm.getReason() != null)
                    return AjaxResult.success(applicationForm.getReason());

                else return AjaxResult.success("管理员并未输入拒绝理由");
            } else return AjaxResult.error("查无此手机号码");
        }

        return AjaxResult.error("请输入手机号码");
    }

    @Override
    public AjaxResult selectOneByOpenId(String openId) {
        QueryWrapper<ApplicationForm> wrapper = new QueryWrapper<>();
        wrapper.eq("open_id", openId);
        ApplicationForm applicationForm = applicationFormMapper.selectOne(wrapper);
        if (applicationForm == null) {
            return AjaxResult.error("您还未申请志愿者");
        }

        return AjaxResult.success(applicationForm);
    }


}
